//保养统计
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          show-summary
          :summary-method="getSummaries"
          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :align="column.align" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, teamlist } from '@/api/equipmentManage/equipmentReport/maintainReport'
import { getDictSelects } from '@/api/common/common'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import showDetail from '@/components/JLINK2/common/ShowDetailColumnThree'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '设备保养统计', // 页面唯一标识
      pageExportId: '设备保养统计导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      totalWord: ['manHour', 'spareNumber', 'money', 'maintainMoney', 'totalSumMoney'],
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      pushColumns: [], // 推送配置实际用的字段参数
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'bookName', label: '设备信息', minWidth: '200', canOrder: false },
        { prop: 'code', label: '保养工单号' },
        { prop: 'planCode', label: '保养计划单号' },
        { prop: 'planTime', label: '计划保养时间', minWidth: '140' },
        { prop: 'completeTime', label: '保养完成时间', minWidth: '140' },
        { prop: 'manHour', label: '保养工时（h）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'levelName', label: '保养级别', canOrder: false },
        { prop: 'baseMaintenanceName', label: '维保班组', canOrder: false },
        { prop: 'shiftLabel', label: '班次', canOrder: false },
        { prop: 'checkPersonName', label: '保养人员', canOrder: false },
        { prop: 'spareNumber', label: '消耗备件数', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'moneyThousandths', label: '备件金额（元）', minWidth: 140, canOrder: false, align: 'right' },
        { prop: 'maintainMoneyThousandths', label: '保养费用（元）', minWidth: 140, canOrder: false, align: 'right' },
        { prop: 'totalSumMoneyThousandths', label: '合计（元）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' }
      ],
      exportTableColumns: [
        { prop: 'bookName', label: '设备信息', minWidth: '200', canOrder: false },
        { prop: 'code', label: '保养工单号' },
        { prop: 'planCode', label: '保养计划单号' },
        { prop: 'planTime', label: '计划保养时间', minWidth: '140' },
        { prop: 'completeTime', label: '保养完成时间', minWidth: '140' },
        { prop: 'manHour', label: '保养工时（h）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'levelName', label: '保养级别', canOrder: false },
        { prop: 'baseMaintenanceName', label: '维保班组', canOrder: false },
        { prop: 'shiftLabel', label: '班次', canOrder: false },
        { prop: 'checkPersonName', label: '保养人员', canOrder: false },
        { prop: 'spareNumber', label: '消耗备件数', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false },
        { prop: 'money', label: '备件金额（元）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' },
        { prop: 'maintainMoney', label: '保养费用（元）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' },
        { prop: 'totalSumMoney', label: '合计（元）', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false, align: 'right' }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        { prop: 'bookId', label: '设备信息', type: 'select', optionsName: 'equipmentList1' },
        // { prop: 'code', label: '保养工单号', type: 'input' },
        // { prop: 'planCode', label: '保养计划单号', type: 'input' },
        { prop: 'level', label: '保养级别', type: 'select', optionsName: 'statusOptions3' },
        { prop: 'maintenanceId', label: '维保班组', type: 'select', optionsName: 'equipmentList' },
        { prop: 'shiftCode', label: '班次', type: 'dict', optionsName: 'statusOptions2' },
        // { prop: 'checkPersonName', label: '保养人员', type: 'input' },
        { prop: 'planDate', label: '计划保养时间', type: 'time', isTime: true, hasDefault: true, defaultKey: 'defaultSearchTime' },
        { prop: 'completeTime', label: '保养完成时间', type: 'time', isTime: true }
      ],
      defaultPushColumns: [// 推送配置常规字段（初始值）
        { prop: 'pushCycle', label: '推送周期', type: 'select', optionsName: 'pushCycleList' },
        { prop: 'sendUsers', label: '推送对象', type: 'pushUser-select', optionsName: 'pushUserList' },
        { prop: 'cancelPush', label: '是否启用', type: 'switch' },
        { prop: '', label: '筛选范围', type: '' },
        { prop: 'bookId', label: '设备信息', type: 'select', optionsName: 'equipmentList1' },
        { prop: 'code', label: '保养工单号', type: 'input' },
        { prop: 'planCode', label: '保养计划单号', type: 'input' },
        { prop: 'level', label: '保养级别', type: 'select', optionsName: 'statusOptions3' },
        { prop: 'maintenanceId', label: '维保班组', type: 'select', optionsName: 'equipmentList' },
        { prop: 'shiftCode', label: '班次', type: 'dict', optionsName: 'statusOptions2' },
        { prop: 'checkPersonName', label: '保养人员', type: 'input' },
        { prop: 'planDate', label: '计划保养时间', type: 'time-select', isTime: true, hasDefault: true, defaultKey: 'defaultSearchTime' },
        { prop: 'completeTime', label: '保养完成时间', type: 'time-select', isTime: true }
      ],
      pushCycleList: [
        { value: '每天', label: '每天' },
        { value: '每周一', label: '每周一' },
        { value: '每月一号', label: '每月一号' }
      ],
      showKey: [ // 查看详情的配置（根据配置自动生成查看页面）
        {
          name: '基本信息',
          columns: [
            { type: 'form', name: '保养工单号', key: 'code', length: 'double' }, // type: 'form' 表示是普通的表单展示
            {
              type: 'table', name: '备件明细', key: 'spareListVos', columns: [ // type: 'table' 表示是表格
                { name: '备件编码', key: 'spareCode' },
                { name: '备件编码', key: 'spareName' },
                { name: '使用数量', key: 'useNumber' },
                { name: '单价', key: 'price', align: 'right' },
                { name: '金额（元）', key: 'allPrice', align: 'right' }
              ]
            }
          ]
        }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '批量导出2', perm: '' },
        { label: '推送配置', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions2: [],
      equipmentList: [],
      defaultSearchTime: [],
      statusOptions3: [
        { value: '', label: '全部' },
        { value: '1', label: '一级保养' },
        { value: '2', label: '二级保养' },
        { value: '3', label: '三级保养' }
      ],
      equipmentList1: [],
      Updata: []
    }
  },
  created() {
    this.defaultSearchTime = [this.$moment().startOf('months').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  mounted() {
    this.loadEquipmentList()
    this.loadteamlList()
    this.getEquipmentStatus()
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'spareNumber') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'spareNumber':
          this.showDetail(row)
          break
      }
    },
    showDetail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: showDetail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            showData: row,
            showKey: this.showKey
          }// props
        },
        area: ['880px', '500px'],
        title: '查看消耗备件', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data.dataTable.data
        this.total = res.data.dataTable.total
        this.Updata = res.data.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    loadEquipmentList() {
      getEquipmentList1().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.deviceName
        })
        this.equipmentList1 = res.data
      })
    },
    // 维保班组
    loadteamlList() {
      teamlist().then(res => {
        res.data.forEach(item => {
          item['value'] = item.id
          item['label'] = item.name
        })
        this.equipmentList = res.data
      })
    },
    // 保养级别字典
    getEquipmentStatus() {
      getDictSelects('shiftCode').then(res => {
        this.statusOptions2 = res.data
      })
    },
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        list(data).then(res => {
          resolve(res.data.dataTable.data)
        })
      })
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'maintainMoneyThousandths':
            sums[index] = this.Updata.totalMaintainMoney
            break
        }
        switch (column.property) {
          case 'totalSumMoneyThousandths':
            sums[index] = this.Updata.totalAllMoney
            break
        }
        switch (column.property) {
          case 'manHour':
            sums[index] = this.Updata.totalManHour
            break
        }
        switch (column.property) {
          case 'spareNumber':
            sums[index] = this.Updata.totalSpareNumber
            break
        }
        switch (column.property) {
          case 'moneyThousandths':
            sums[index] = this.Updata.totalMoneyThousandths
            break
        }
      })
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: rgba(0,0,0,0);
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  border-bottom: 1px solid #dfe6ec;
}

</style>
